package io.yizoo.business.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.yizoo.business.entity.biz_hrm_ei_basic;
import io.yizoo.business.entity.biz_hrm_ei_rel_org;
import io.elm.core.CoreController;
import io.elm.core.HMapper;
import io.yizoo.system.entity.sys_org;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/business/hrm/ei")
public class hrm_eiController extends CoreController {

    @Override
    protected void Init() {
        PowerEntity = biz_hrm_ei_basic.class;
        PowerUse = EnumSet.of(Power.IDDelete, Power.IDAny, Power.IDGetSingle,Power.IDUpsert);
    }

    @PostMapping("/GetData")
    public Object GetData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_hrm_ei_basic.class)
                        .IsWhere("and (name like concat('%', #{name}, '%') or emp_code like concat('%', #{name}, '%'))", "name")
                        .IsWhere("and #{org} in (select org_id from biz_hrm_ei_rel_org where ei_id = biz_hrm_ei_basic.id)","org")
                        .OrderBy("emp_code")
                        .IsPage()
                        .GetFactor());
        return JsonResultPage(ret);
    }

    @PostMapping("/Any")
    public Object Any(@RequestBody String jsonStr) {
        return JsonResult(Ctor.HMapper.Any(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_hrm_ei_basic.class)
                        .Where("and emp_code=#{emp_code} and id<>#{id}", "emp_code", "id")
                        .GetFactor()
        ));
    }

    @PostMapping("/Upsert")
    public Object Upsert(@RequestBody String jsonStr) throws Exception {
        ObjectNode node = Ctor.getObjectNode(jsonStr);
        String id = Ctor.HMapper.Upsert(biz_hrm_ei_basic.class, (ObjectNode) node.get("mod"),null);
        List<biz_hrm_ei_rel_org> entitys=new ArrayList<>();
        for (JsonNode jsonNode : (ArrayNode) node.get("orgs")) {
             biz_hrm_ei_rel_org entity = new biz_hrm_ei_rel_org();
            entity.setEi_id(id);
            entity.setOrg_id(jsonNode.get("id").asText());
            entity.setFzr(jsonNode.get("fzr").asInt());
            entity.setPm(jsonNode.get("pm").asInt());
            entitys.add(entity);
        }
        Ctor.HMapper.Save(entitys,Ctor.DMaker().Deleter
                .putFactorItem("id",id)
                 .From(biz_hrm_ei_rel_org.class)
                .Where("and ei_id=#{id}","id")
                .GetFactor());
        return JsonResult(id != null ? 1 : 0);
    }

    @PostMapping("/Delete")
    public Object Delete(@RequestBody String jsonStr) throws Exception {
        int rows = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_hrm_ei_basic.class)
                        .Where("and id=#{id}", "id")
                        .GetFactor());
        Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_hrm_ei_rel_org.class)
                        .Where("and ei_id=#{id}", "id")
                        .GetFactor());
        return JsonResult(rows);
    }

    @PostMapping("/GetOrgs")
    public Object GetOrgs(@RequestBody String jsonStr) {
        List<Map<String,Object>> ret=new ArrayList<>();
        List<Map<String,Object>> rels=Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_hrm_ei_rel_org.class)
                        .Where("and ei_id=#{ei_id}","ei_id")
                        .OrderBy("pm desc")
                        .GetFactor()
        );
        for (Map<String,Object> rel : rels) {
            List<Map<String,Object>> orgs = Ctor.HMapper.GetPath(
                    Ctor.DMaker().Selector
                            .From(sys_org.class)
                            .putFactorItem("id",rel.get("org_id").toString())
                            .Where("and id=#{id}", "id")
                            .GetFactor());
            String org_name="";
            for(Map<String,Object> org : orgs) {
                org_name = org.get("name").toString() + (org_name==""?"":"/") + org_name;
            }
            Map<String,Object> org=new HashMap<>();
            org.put("name",org_name);
            org.put("id",rel.get("org_id"));
            org.put("fzr",rel.get("fzr"));
            org.put("pm",rel.get("pm"));
            ret.add(org);
        }
        return JsonResult(ret);
    }

    @PostMapping("/GetOrgTree")
    public Object GetOrgTree(@RequestBody String jsonStr) throws Exception {
        Map<String, Object> factor = Ctor.DMaker(jsonStr).Selector
                .From(sys_org.class)
                .IsWhere("and name like CONCAT('%', #{name}, '%')","name")
                .IsPage()
                .GetFactor();
        HMapper.PageData data = Ctor.HMapper.GetTree(factor);
        return JsonResult(data);
    }

}
